/**
 * Created with webstrom
 * author:金海宾
 * Date:2017/9/5
 * Time:16:47
 */
import {Message} from 'element-ui';

/**
 * 获取站点信息
 * @returns {{server: string, port: string, sitename: string}}
 */
function getDomain() {
    var url = window.location.href;
    var reg = /http:\/\/([^\/:]+)(:[0-9]+)?\/([^\/]+)/gi;
    reg.test(url);
    return {
        server: RegExp.$1,
        port: RegExp.$2,
        sitename: RegExp.$3
    };
}

/**
 * 复制数据
 * @param data
 * @param count
 */
function copyData(data, count) {
    let array = [];
    count = count || 50;
    for (let i = 0; i < count; i++) {
        array.push(Object.assign({}, data));
    }
    return array;
}

function Validate() {
    this.validFunMap = {};
    const RuleMap = {
        'phone': {
            reg: /^1\d{10}$/,
            msg: '请输入格式正确的的手机号'
        },
        'email': {
            reg: /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/,
            msg: '请输入格式正确的邮箱'
        },
        'idcard': {
            reg: /^\d{17}[\d|X]$/,
            msg: '请输入格式正确的身份证号'
        },
        pwd: {
            reg: /^[\w]{6,20}$/,
            msg: '密码不能小于六位'
        },
        money: {
            reg: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            msg: '金额只能为数字'
        },
        decimal: {
            reg: /^[0-9]+([.]{1}[0-9]{1,2})?$/,
            msg: '整数或者小数保留2位'
        },
        space: {
            reg: /^[^\s]*$/,
            msg: '不能包含空格'
        },
        name: {
            reg: /^[A-Za-z][A-Za-z_0-9]{5,19}$/,
            msg: '6到20位的以字母开头的数字字母下划线'
        },
        productCode: {
            reg: /^[A-Za-z][A-Za-z_0-9-]{3,19}$/,
            msg: '4-20位以字母开头的数字字母下划线横线\n'
        },
        moduleCode: {
            reg: /^[A-Za-z][A-Za-z_0-9-]{1,19}$/,
            msg: '2-20位以字母开头的数字字母下划线横线\n'
        }
    };
    for (let key in RuleMap) {
        this.validFunMap[key] = (rule, value, callBack) => {
            let ruleObj = RuleMap[key];
            if (value !== 0 && !value || !ruleObj['reg'].test(value)) {
                callBack(new Error(ruleObj['msg']));
            } else {
                callBack();
            }
        };
    }
    this.getValid = (validName) => {
        return this.validFunMap[validName] || '';
    };
}

/**
 * 生成API KEy
 * @returns {string}
 * @constructor
 */
function APIKEY() {
    var $chars = 'ABCDEF0123456789';
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < 32; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

/**
 * 获取 前n天的时间
 * @param count
 */
function getPreDay(count) {
    return new Date(new Date().getTime() - 60 * 60 * 24 * 1000 * count);
}

/**
 * 获取前几个月
 * @param count
 * @returns {Date}
 */
function getPreMonth(count) {
    let now = new Date();
    now.setMonth(now.getMonth() - count);
    return now;
}

/**
 * 对象转数组
 * @param map
 */
function mapToArray(map) {
    let array = [];
    for (let key in map) {
        array.push({
            key: key,
            value: map[key]
        });
    }
    return array;
}

/**
 * 获取站点配置信息
 */
function getConfig() {
    return Object.assign({}, process.env.SITE);
}

/**
 * 去往登录页
 */
function toLogin() {
    Message('登录超时，请重新登录', {
        type: 'warning'
    });
    setTimeout(function () {
        document.location.replace('./index.html');
    }, 1500);
}

export default {getDomain, getConfig, copyData, Validate, APIKEY, getPreDay, getPreMonth, mapToArray, toLogin};
